package yanjun.tool.validate.util;

import org.springframework.util.Assert;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 反射工具类
 *
 * @author rainn
 * @date 2019-04-24
 */
public class ReflectUtils {

    /**
     * 指定class是否存在指定的annotation
     *
     * @param clazz 指定class
     * @param annotaionClazz annotaionClazz
     * @return 是否存在
     */
    public static boolean hasAnnotation(Class<?> clazz,Class<? extends Annotation> annotaionClazz)
    {
        if(clazz==null)
        {
            throw new IllegalArgumentException("argument clazz must be not null");
        }

        if(annotaionClazz==null)
        {
            throw new IllegalArgumentException("argument annotaionClazz must be not null");
        }

        Annotation annotation = clazz.getAnnotation(annotaionClazz);
        return annotation!=null;
    }

    /**
     * 指定方法是否存在指定的annotation申明的参数
     *
     * @param method 指定方法
     * @param annotaionClazz 指定的annotation
     * @return 是否存在
     */
    public static boolean hasAnnotationArgument(Method method,Class<? extends Annotation> annotaionClazz)
    {
        if(method==null)
        {
            throw new IllegalArgumentException("argument method must be not null");
        }

        if(annotaionClazz==null)
        {
            throw new IllegalArgumentException("argument annotaionClazz must be not null");
        }

        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (Annotation[] annotations:parameterAnnotations)
        {
            for (Annotation annotation:annotations)
            {
                if(annotation.annotationType()==annotaionClazz)
                {
                    return true;
                }
            }
        }
        return false;
    }
}
